package com.example.utils;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.bluetooth.MyBlueToothService;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class BlueToothServiceUtils extends UniModule {
    public static UniJSCallback callback;

    @UniJSMethod
    public static void startBlueTooth (Context context) {
//        bluePermission(activity,context);
        context.startService(new Intent(context, MyBlueToothService.class));
    }
    @UniJSMethod
    public static void stopBlueTooth (Context context) {
        context.stopService(new Intent(context, MyBlueToothService.class));
    }
    private static boolean bluePermission(Activity activity,Context context){
        //compileSdkVersion项目中编译SDK版本大于30申请以下权限可使用
        //Manifest.permission.BLUETOOTH_SCAN、Manifest.permission.BLUETOOTH_ADVERTISE、Manifest.permission.BLUETOOTH_CONNECT
        //若小于30可以直接使用权限对应的字符串
        if (Build.VERSION.SDK_INT>30){
            if (ContextCompat.checkSelfPermission(context,
                    "android.permission.BLUETOOTH_SCAN")
                    != PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(context,
                    "android.permission.BLUETOOTH_ADVERTISE")
                    != PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(context,
                    "android.permission.BLUETOOTH_CONNECT")
                    != PERMISSION_GRANTED|| ContextCompat.checkSelfPermission(context,
                    "android.permission.ACCESS_COARSE_LOCATION")
                    != PERMISSION_GRANTED|| ContextCompat.checkSelfPermission(context,
                    "android.permission.ACCESS_FINE_LOCATIONpermission")
                    != PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(activity,new String[]{
                        "android.permission.BLUETOOTH_SCAN",
                        "android.permission.BLUETOOTH_ADVERTISE",
                        "android.permission.BLUETOOTH_CONNECT",
                        "android.permission.ACCESS_COARSE_LOCATION",
                        "android.permission.ACCESS_FINE_LOCATIONpermission"}, 1);
                return false;
            }
        }
        return true;
    }
}